package com.jianyun.jyzs.model;

import android.util.Log;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.EngineerProjectResult;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.dao.EngProjectDao;
import com.jianyun.jyzs.dao.EngineerDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.model.imdoel.IEngineerModel;
import com.jianyun.jyzs.utils.LoginCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangshijun on 2018/5/30.
 */

public class EngineerModel implements IEngineerModel {
    private static EngineerModel model;

    private EngineerModel() {
    }

    public static EngineerModel getInstance() {
        if (model == null) {
            model = new EngineerModel();
        }
        return model;
    }

    /**
     * 获取工程动态列表
     *
     * @param dao
     * @param userid
     * @param enterpriseCode
     * @param onGetEngineerListener
     */
    @Override
    public void getEngineer(final EngineerDao dao, String userid, String enterpriseCode, String upOrDown, final OnGetEngineerListener onGetEngineerListener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("method", "GeProjectProgress");
        map.put("userId", userid);
        map.put("enterpriseCode", enterpriseCode);
        map.put("upOrDown", upOrDown);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        api.getNewEngineerDynamic(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineeringResult>() {
                    @Override
                    protected void onResponse(EngineeringResult response) {
                        Log.i("test", "服务器返回的：" + response.getList().toString());
                        dao.delEngineerAll();
                        if (response.isResult()) {
                            Gson gson = new Gson();
                            List<EngineeringResult.EngineerBean> list = response.getList();

                            for (EngineeringResult.EngineerBean ee : list
                                    ) {
                                ee.setListAgress(gson.toJson(ee.getAgress()));
                                ee.setListAttachment(gson.toJson(ee.getAttachment()));
                                ee.setListDisscuss(gson.toJson(ee.getDiscuss()));

                                List<EngineeringResult.EngineerBean.AttachmentBean> attachment = ee.getAttachment();
                                ArrayList<String> images = new ArrayList<>();
                                for (EngineeringResult.EngineerBean.AttachmentBean eea : attachment
                                        ) {
                                    if (eea.getPhotoPath() != null) images.add(eea.getId());
                                }
                                ee.setListImages(gson.toJson(images));
                                ee.setDeleted(response.isDeleted());
                                dao.insert(ee);
                            }
                            onGetEngineerListener.onSuccess();
                        } else {

                            onGetEngineerListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onGetEngineerListener.onFailed(msg);
                    }
                });
    }


    /**
     * 获取工程动态列表
     *
     * @param dao
     * @param userid
     * @param enterpriseCode
     * @param onGetEngineerListener
     */
    @Override
    public void getRefreshEngineer(final EngineerDao dao, String userid, String enterpriseCode, String upOrDown, final OnRefeshListener onRefeshListener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("method", "GeProjectProgress");
        map.put("userId", userid);
        map.put("enterpriseCode", enterpriseCode);
        map.put("upOrDown", upOrDown);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        api.getNewEngineerDynamic(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineeringResult>() {
                    @Override
                    protected void onResponse(EngineeringResult response) {
                        Log.i("test", "服务器返回的：" + response.getList().toString());
                        if (response.isResult()) {
                            Gson gson = new Gson();

                            List<EngineeringResult.EngineerBean> list = response.getList();
                            for (EngineeringResult.EngineerBean ee : list
                                    ) {
                                ee.setListAgress(gson.toJson(ee.getAgress()));
                                ee.setListAttachment(gson.toJson(ee.getAttachment()));
                                ee.setListDisscuss(gson.toJson(ee.getDiscuss()));
                                List<EngineeringResult.EngineerBean.AttachmentBean> attachment = ee.getAttachment();
                                ArrayList<String> images = new ArrayList<>();
                                for (EngineeringResult.EngineerBean.AttachmentBean eea : attachment
                                        ) {
                                    if (eea.getPhotoPath() != null) images.add(eea.getId());
                                }
                                ee.setListImages(gson.toJson(images));
                                ee.setDeleted(response.isDeleted());
                                dao.insert(ee);
                            }
                            onRefeshListener.onSuccess();
                        } else {
                            onRefeshListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onRefeshListener.onFailed(msg);
                    }
                });
    }

    /**
     * 刷新数据
     *
     * @param dao
     * @param userid
     * @param enterpriseCode
     * @param direction
     * @param onRefeshListener
     */
    @Override
    public void refeshList(final EngineerDao dao, String userid, String enterpriseCode, String direction, String compareId, String proJectId, String placeName, final OnRefeshListener onRefeshListener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        HashMap<String, String> map = new HashMap<>();
        map.put("method", "GeProjectProgress");
        map.put("userId", userid);
        map.put("enterpriseCode", enterpriseCode);
        map.put("upOrDown", direction);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
        if ("0".equals(direction)) {
            map.put("compareId", compareId);
        }
        if (proJectId != null) {
            map.put("projectId", proJectId);
        }
        if (placeName != null) {
            map.put("placeName", placeName);
        }
        Log.i("test", map.toString());
        api.getNewEngineerDynamic(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineeringResult>() {
                    @Override
                    protected void onResponse(EngineeringResult response) {
                        if (response.isResult()) {
                            Gson gson = new Gson();

                            List<EngineeringResult.EngineerBean> list = response.getList();
                            for (EngineeringResult.EngineerBean ee : list
                                    ) {
                                ee.setListAgress(gson.toJson(ee.getAgress()));
                                ee.setListAttachment(gson.toJson(ee.getAttachment()));
                                ee.setListDisscuss(gson.toJson(ee.getDiscuss()));
                                List<EngineeringResult.EngineerBean.AttachmentBean> attachment = ee.getAttachment();
                                ArrayList<String> images = new ArrayList<>();
                                for (EngineeringResult.EngineerBean.AttachmentBean eea : attachment
                                        ) {
                                    if (eea.getPhotoPath() != null) images.add(eea.getId());
                                }
                                ee.setListImages(gson.toJson(images));
                                ee.setDeleted(response.isDeleted());
                                dao.insert(ee);
                            }
                            onRefeshListener.onSuccess();
                        } else {
                            onRefeshListener.onFailed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onRefeshListener.onFailed(msg);
                    }
                });
    }

    @Override
    public void getP1Data(final EngProjectDao dao, HashMap<String, String> map, final OnP1DataListener onP1DataListener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        final String enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        api.getNewEngineerProject(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineerProjectResult>() {
                    @Override
                    protected void onResponse(EngineerProjectResult response) {
                        if (response.isResult()) {
                            List<EngineerProjectResult.EngProject> list = response.getList();
                            for (EngineerProjectResult.EngProject ep : list) {
                                dao.insert(ep);
                            }
                            EngineerProjectResult.EngProject project = new EngineerProjectResult.EngProject();
                            project.setProJectName("全部项目");
                            project.setCount(10000);
                            project.setEnterpriseCode(enterpriseCode);
                            project.setAll(true);
                            dao.insert(project);
                            onP1DataListener.onP1Success();
                        } else {
                            onP1DataListener.onP1Failed(response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        onP1DataListener.onP1Failed(msg);
                    }
                });
    }
}
